*******************************************************************************
* Load data
*******************************************************************************

use dataset_gss, clear

*******************************************************************************
* Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

preserve

keep if subgroup == "`subgroup'"

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local legend1 legend(off)
local legend2 legend(off)
if "`subgroup'" == "All" {
	local legend1 legend(order(1 ///
		"{fontface monospace:{bf:Support Police Spending}}" ///
		2 "{fontface monospace:{bf:Synthetic Control}}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none) fcol(none)))
	local legend2 legend(order(1 "{fontface monospace:{bf:Difference}}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)))
}

*******************************************************************************
* Make lags
*******************************************************************************

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen nat`i'm = mean(nat) if year == `i', by(target)
	egen nat`i' = mean(nat`i'm), by(target)
	drop nat`i'm
	local lags `lags' nat`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "crimy"

*******************************************************************************
* Synth
*******************************************************************************

synth nat `lags', trunit(`r(mean)') trperiod(2014) keep(tmp, replace)

use tmp, clear
erase tmp.dta

*******************************************************************************
* Graph
*******************************************************************************

#delimit;

gr tw
	(line _Y_treated _time, col(black) lwid(thick))
	(line _Y_synthetic _time, col(gray) lwid(thick))
	,
		`legend1'
		name(g1, replace)
		xline(2014, lpat(dash))
		ylab(-0.2(0.2)0.8, angle(horiz))
		plotregion(style(none))
		ytitle("")
		xtitle("")
		xlab(1982(8)2022)		
		;

#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

gr close

gen d = _Y_treated - _Y_synthetic

#delimit;

gr tw
	(line d _time, col(black) lwid(thick))
	(sc d _time, msym(none))
	,
		`legend2'
		name(g2, replace)
		yline(0, lpat(shortdash))
		xline(2014, lpat(dash))
		plotregion(style(none))
		ylab(
			-.6 "-.6"
			-.4 "-.4"
			-.2 "-.2"
			0 "0"
			.2 ".2"
			.4 ".4"
			.6 ".6"
			,
				angle(horiz)
		)
		ytitle("")
		xtitle("")
		xlab(1982(8)2022)		
		;

#delimit cr

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine g1 g2, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* Close subgroup loop
*******************************************************************************

restore

}

*******************************************************************************
* Combine graphs
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_gss_sc.pdf", replace

*******************************************************************************
* Get substantive effects
*******************************************************************************

use dataset_gss, clear

collapse M=nat (sd) SD=nat if year < 2014, by(target subgroup)

su M SD if target == "crimy" & subgroup == "Democrat"
su M SD if target != "crimy" & subgroup == "Democrat"

su M SD if target == "crimy" & subgroup == "Republican"
su M SD if target != "crimy" & subgroup == "Republican"

*******************************************************************************
* End
*******************************************************************************
